package com.dd.shop4j.website.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dd.shop4j.common.utils.Message;
import com.dd.shop4j.common.utils.Message.MessageType;
import com.dd.shop4j.goods.model.GoodsInfo;
import com.dd.shop4j.website.controller.forms.ShopcarItem;
import com.dd.shop4j.website.goods.service.GoodsInfoService;

@Controller
@RequestMapping("/shopcar")
public class ShopcarController {

	private static final String SHOPCAR = "SHOPCAR";
	
	@Resource private GoodsInfoService goodsInfoService;
	
	@RequestMapping
	public ModelAndView init(HttpSession session) {
		ModelAndView view = new ModelAndView("/website/shopcar");
		List<ShopcarItem> items = (List<ShopcarItem>) session.getAttribute(SHOPCAR);
		if (items == null) {
			items = new ArrayList<ShopcarItem>();
		}
		view.addObject("items", items);
		return view;
	}
	
	@RequestMapping("/add")
	@SuppressWarnings("unchecked")
	public ModelAndView add(Integer goodId, Integer count,
			HttpSession session) {
		ModelAndView view = new ModelAndView("/website/shopcar");
		if (goodId == null) {
			Message message = new Message(MessageType.WEBSITE_FAILURE);
			message.setGotoPage("");
			message.setContext("商品id不能为空");
			return message.getView();
		}
		if (count == null || count.intValue() <= 0) {
			count = 1;
		}
		GoodsInfo goodsInfo = goodsInfoService.getGoodsInfo(goodId);
		if (goodsInfo == null) {
			Message message = new Message(MessageType.WEBSITE_FAILURE);
			message.setContext("没有找到商品信息");
			return message.getView();
		}
		List<ShopcarItem> items = (List<ShopcarItem>) session.getAttribute(SHOPCAR);
		if (items == null) {
			items = new ArrayList<ShopcarItem>();
		}
		boolean flag = true;
		for (ShopcarItem item : items) {
			if (item.getGoodId().intValue() == goodId.intValue()) {
				item.setCount(item.getCount() + count);
				flag = false;
				break;
			}
		}
		if (flag) {
			ShopcarItem item = new ShopcarItem();
			item.setCount(count);
			item.setGoodId(goodId);
			item.setGoodsInfo(goodsInfo);
			items.add(item);
			session.setAttribute(SHOPCAR, items);
		}
		view.addObject("items", items);
		return view;
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(Integer goodId, HttpSession session) {
		ModelAndView view = new ModelAndView("/website/shopcar");
		if (goodId == null) {
			Message message = new Message(MessageType.WEBSITE_FAILURE);
			message.setGotoPage("");
			message.setContext("商品id不能为空");
			return message.getView();
		}
		List<ShopcarItem> items = (List<ShopcarItem>) session.getAttribute(SHOPCAR);
		if (items == null) {
			items = new ArrayList<ShopcarItem>();
		}
		for (ShopcarItem item : items) {
			if (item.getGoodId().intValue() == goodId.intValue()) {
				items.remove(item);
				break;
			}
		}
		session.setAttribute(SHOPCAR, items);
		view.addObject("items", items);
		return view;
	}
	
}
